<!--

业主代表按照计划检查结果

-->

<template>
  <my-dialog :subLoading="subLoading" @on-submit="submitHandle" :readOnly="readOnly">
    <my-header slot="header"
               project-sub-text="按照计划检查结果"  :project-sub-title="`业主代表检查 检查日期：${nowTime}`"
               :project-name="projectDetail.project_name || ''"  ></my-header>
    <div slot="body">
      <widgets title="施工单位" :value="projectDetail.construction_organeation|| ''" icon="organization"></widgets>
      <widgets title="项目负责人" :value="projectDetail.project_manager || ''" icon="responsibility"></widgets>
      <widgets title="监理单位" :value="projectDetail.jianli_company || ''" icon="supervisor"></widgets>
      <widgets title="监理项目负责人" :value="projectDetail.jianli_name || ''" icon="responsibility"></widgets>
      <widgets title="标段名称" :value="projectDetail.sction_name|| ''" icon="section"></widgets>
      <my-textarea v-model="process" :disabled="readOnly" label-text="工程量完成情况"
                   placeholder="请填写工程量完成情况" error-message="请填写工程量完成情况"></my-textarea>
      <my-textarea v-model="power" :disabled="readOnly" label-text="人员及机械投入情况"
                   placeholder="请填写人员及机械投入情况" error-message="请填写人员及机械投入情况"></my-textarea>
      <my-textarea v-model="dayConditions" :disabled="readOnly" label-text="日，周，月进度完成情况"
                   placeholder="请填写日，周，月进度完成情况" error-message="请填写日，周，月进度完成情况"></my-textarea>
      <my-textarea v-model="audit" :disabled="readOnly" label-text="施工，监理管理人员在岗情况"
                   placeholder="请填写施工，监理管理人员在岗情况" error-message="请填写施工，监理管理人员在岗情况"></my-textarea>
      <my-textarea v-model="compare" :disabled="readOnly" label-text="进度计划与实际进度的对比情况"
                   placeholder="请填写进度计划与实际进度的对比情况" error-message="请填写进度计划与实际进度的对比情况" ></my-textarea>
      <my-textarea v-model="delay" :disabled="readOnly" label-text="进度延误分析（非必填）"
                   placeholder="请填写进度延误分析" :required="false"></my-textarea>
      <check-box v-model="isPunish" :disabled="readOnly" lbl-text="是否满足延误进度处罚条件"></check-box>
      <upload v-if="!readOnly" load-msg="上传附件" format-restrict="IMAGE,VIDEO,DOCUMENT" @loadedFile="uploadSuccessHandle" @onDelete="deleteSuccessHandle" inner-text="此处选择照片上传,视频,处罚合同的word等文件"></upload>
      <down v-else :files="projectFiles"></down>
    </div>
  </my-dialog>
</template>

<script>

    import Dialog from "@/components/Dialog";
    import Header from "@/components/Dialog/Header";
    import Widgets from "@/components/Dialog/Widgets";
    import TextArea from "@/components/Dialog/TextArea";
    import Upload from "@/components/Dialog/Load";
    import Down from  "@/components/Dialog/Down";
    import CheckBox from "@/components/Dialog/Check";
    import  { mapGetters } from "vuex";
    import moment from 'moment'
    import taskApi from '@/api/task'
    import projectApi from '@/api/project'
    moment.locale('zh-CN')
    import _ from 'lodash'

    export default {
      name:"OwnerCheckPlan",
      components:{
        'MyDialog':Dialog,
        'MyHeader':Header,
         Widgets,
        'MyTextarea':TextArea,
        CheckBox,
        Upload,
        Down
      },
      computed: {
        ...mapGetters([
          'userData'
        ]),
        nowTime () {
          if (this.readOnly) {
            return this.planCreated
          }
          return moment().format('YYYY-MM-DD HH:mm')
        },
        projectFiles () {
          return _.filter(this.projectDetail.file, file => {
            return file.type === 4
          })
        }
      },
      data(){
        return {
          subLoading: false,
          readOnly: false,
          isPunish:false,
          process:"",
          power:"",
          audit:"",
          compare:"",
          delay:"",
          files: [],
          dayConditions: '',
          projectDetail: {
            file: []
          },
          taskDetail: {},
          planCreated: ''
        }
      },
      created () {
        this.getProjectInfo()
        this.getTaskInfo(() => {
          this.getCheckPlanDetail()
        })
      },
      methods: {
        getProjectInfo () {
          const projectId = this.$route.params.projectId
          this.$store.dispatch('getSingeProject', {token: this.userData.token, pId: projectId}).then(res => {
            this.projectDetail = res.data
          }, (res) => {
            this.$message(`数据请求失败,错误信息:${JSON.stringify(res)}`)
          }).catch(err => {
            this.$message(`系统错误，请求数据错误，错误信息:${JSON.stringify(err)}`)
          })
        },
        getTaskInfo (cb) {
          const taskId = this.$route.params.taskId
          taskApi.getTaskInfo(this.userData.token, taskId).then(res => {
            const result = res.data
            this.taskDetail = result.data
            if (cb) {
              cb()
            }
          }, (res) => {
            this.$message(`数据请求失败,错误信息:${JSON.stringify(res)}`)
          }).catch(err => {
            this.$message(`系统错误，请求数据错误，错误信息:${JSON.stringify(err)}`)
          })
        },
        getCheckPlanDetail () {
          const checkPlanId = this.taskDetail.check_plan_id
          projectApi.getCheckPlanDetail(this.userData.token, checkPlanId).then(res => {
            const result = res.data.data
            if (result.ownerExamineIsfinish.data.id) {
              this.readOnly = true
              this.planCreated = result.ownerExamineIsfinish.data.created_at
              this.isPunish = (result.ownerExamineIsfinish.data.is_approve === 0)
              this.process = result.ownerExamineIsfinish.data.work_performance
              this.power = result.ownerExamineIsfinish.data.people_condition
              this.dayConditions = result.ownerExamineIsfinish.data.day_condition
              this.audit = result.ownerExamineIsfinish.data.people_post
              this.compare = result.ownerExamineIsfinish.data.progress_condition
              this.delay = result.ownerExamineIsfinish.data.analyze || ''
            }
          })
        },
        submitHandle () {
          if (!this.process) {
            this.$message.error('请填写工程量完成情况')
            return
          }
          if (!this.power) {
            this.$message.error('请填写人员及机械投入情况')
            return
          }
          if (!this.dayConditions) {
            this.$message.error('请填写日，周，月进度完成情况')
            return
          }
          if (!this.audit) {
            this.$message.error('请填写施工，监理管理人员在岗情况')
            return
          }
          if (!this.compare) {
            this.$message.error('请填写进度计划与实际进度的对比情况')
            return
          }
          this.subLoading = true
          const projectId = this.$route.params.projectId
          const taskId = this.$route.params.taskId
          const checkPlanId = this.taskDetail.check_plan_id
          taskApi.ownerCheckFinish(this.userData.token, {
            task_id: taskId,
            project_id: projectId,
            check_plan_id: checkPlanId,
            is_approve: this.isPunish ? 0 : 1,
            users_id: this.projectDetail.users_id,
            work_performance: this.process,
            people_condition: this.power,
            day_condition: this.dayConditions,
            people_post: this.audit,
            progress_condition: this.compare,
            analyze: this.delay,
            file: this.files
          }).then(res => {
            this.$message.success('提交成功')
            this.subLoading = false
            this.$router.back()
          }, (res) => {
            this.$message(`数据请求失败,错误信息:${JSON.stringify(res)}`)
          }).catch(err => {
            this.$message(`系统错误，请求数据错误，错误信息:${JSON.stringify(err)}`)
          })
        },
        uploadSuccessHandle ({data}) {
          this.files.push({id: data.id})
        },
        deleteSuccessHandle ({id}) {
          _.remove(this.files, o => {
            return o.id === id
          })
        }
      }
    }
</script>
